package com.bjc.sdx.adapter;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bjc.sdx.R;
import com.bjc.sdx.activity.MyFragmentActivity;

import org.xmlpull.v1.XmlPullParser;

import java.util.List;
import java.util.Map;

public class MyBaseAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;
    private Context context;
    private static final String TAG = "MyBaseAdapter";
    private PopupWindow popupWindow;

    public MyBaseAdapter(Context context, List<Map<String, Object>> mData){
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.mData = mData;
    }

    /**
     * listview中点击按键弹出对话框
     */
    public void showInfo(){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("我的listview")
                .setMessage("介绍...")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }

    public void showPopupWindow(View view, String text){
        TextView textView = new TextView(mInflater.getContext());
        textView.setText(text);
        textView.setTextSize(30);
        textView.setFocusable(true);
        textView.setFocusableInTouchMode(true);
        textView.setClickable(true);
        textView.setBackgroundResource(R.color.white);
        textView.setSingleLine(true);
        textView.setHorizontallyScrolling(true);
        textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        textView.setMarqueeRepeatLimit(6);
        textView.setGravity(View.TEXT_ALIGNMENT_CENTER);
        textView.setShadowLayer(3,10, 10, R.color.teal_700);
        final PopupWindow popupWindow = new PopupWindow(textView, 400, 150);
        popupWindow.setOutsideTouchable(true); //设置点击外部区域可以取消popupWindow
        view = mInflater.inflate(R.layout.vlist2, null);
        Button button = view.findViewById(R.id.view_btn);
//        popupWindow.showAsDropDown(button);  //设置popupWindow显示,并且告诉它显示在那个View下面
        popupWindow.showAsDropDown(button,button.getWidth(),-button.getHeight(), -100);
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        if (convertView == null) {
            holder= new ViewHolder();
            convertView = mInflater.inflate(R.layout.vlist2, null);
            holder.img = (ImageView)convertView.findViewById(R.id.img);
            holder.title = (TextView)convertView.findViewById(R.id.title);
            holder.info = (TextView)convertView.findViewById(R.id.info);
            holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
            convertView.setTag(holder);
        }else {
            holder =(ViewHolder)convertView.getTag();
        }
        holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
        holder.title.setText((String)mData.get(position).get("title"));
        holder.info.setText((String)mData.get(position).get("info"));

        holder.viewBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> index = mData.get(position);
                if (index.get("info").equals("Toast")){
                    Log.w(TAG, "什么也不做");
                    Toast.makeText(context.getApplicationContext(),"我是一个Toast" ,Toast.LENGTH_LONG).show();
                }else if(index.get("info").equals("AlertDialog")){
                    showInfo();
                }else{
                    String text = (String) index.get( "info");
                    text = "你点击了" + text;
                    showPopupWindow(v, text);
                }
                Log.w(TAG, "你点击了侧边栏" + index);
            }
        });


        return convertView;
    }

    static class ViewHolder{
        public ImageView img;
        public TextView title;
        public TextView info;
        public Button viewBtn;
    }

    private void showPopupWindow(){
        //设置contentView 对于加载 popuplayout 的layout
        View contentView = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.popupwindow_layout, null);
        popupWindow =new PopupWindow(contentView,
                600, ActionBar.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setContentView(contentView);

        //设置各个控件的点击响应   记住，你的控件在哪个layout里面，就用哪个layout的对象去找
        TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);
        Log.w(TAG, "你点击了侧边栏的tv1" + tv1);
        TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);
        TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);

        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context.getApplicationContext(),"clicked computer",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context.getApplicationContext(),"clicked financial",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context.getApplicationContext(),"clicked manage",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

        //显示PopupWindow
        Activity activity = new MyFragmentActivity();
        contentView = mInflater.inflate((XmlPullParser) activity,null, false);
        popupWindow.showAtLocation(contentView, Gravity.AXIS_PULL_BEFORE, 0, 0);
    }



}